<?php

require_once( 'mvc/mvc-exception.php' );

require_once( 'flexical/model/query.php' );

require_once( 'flexical/admin/request-factory.php' );
require_once( 'flexical/admin/request.php' );
require_once( 'flexical/admin/query-request.php' );
require_once( 'flexical/admin/object-builder.php' );

class Flexical_Admin_Query_Request_Factory extends Flexical_Admin_Request_Factory
{
	public function get_request()
	{
		$request = null;

		$request_type = $this->get_request_type( $_REQUEST );

		switch ( $request_type )
		{
			case 'show_queries':
				$request = new Flexical_Admin_Show_Queries_Request();
				break;

			case 'create_query':
				$request = new Flexical_Admin_Create_Query_Request( $this->build_query( $_REQUEST ) );
				break;

			case 'edit_query':
				$request = new Flexical_Admin_Edit_Query_Request( $this->build_query( $_REQUEST ) );
				break;

			case 'update_query':
				$request = new Flexical_Admin_Update_Query_Request( $this->build_query( $_REQUEST ) );
				$request->cancel_selected = $this->get_cancel_selected( $_REQUEST );
				break;

			case 'confirm_delete_query':
				$request = new Flexical_Admin_Confirm_Delete_Query_Request( $this->build_query( $_REQUEST ) );
				break;

			case 'delete_query':
				$request = new Flexical_Admin_Delete_Query_Request( $this->build_query( $_REQUEST ) );
				$request->cancel_selected = $this->get_cancel_selected( $_REQUEST );
				break;

			case 'delete_all_queries':
				$request = new Flexical_Admin_Delete_All_Queries_Request( $this->get_confirmation( $_REQUEST ) );
				break;

			default:
				throw new MVC_Exception( "Unrecognized request type: " . $request_type );
		}

		return $request;
	}

	private $query_builder;

	private function initialize_query_builder()
	{
		$map = array(
			'query_id' => new Flexical_Admin_Object_Property_Spec( 'id', 'int' ),
			'query_title' => new Flexical_Admin_Object_Property_Spec( 'title', 'string' ),
			'query_futureevents' => new Flexical_Admin_Object_Property_Spec( 'futureevents', 'boolean' ),
			'query_recurrence_expansion_start' => new Flexical_Admin_Object_Property_Spec( 'recurrence_expansion_start', 'string' ),
			'query_recurrence_expansion_end'   => new Flexical_Admin_Object_Property_Spec( 'recurrence_expansion_end', 'string' ),
			'query_singleevents' => new Flexical_Admin_Object_Property_Spec( 'singleevents', 'boolean' ),
			'query_start_min' => new Flexical_Admin_Object_Property_Spec( 'start_min', 'string' ),
			'query_start_max' => new Flexical_Admin_Object_Property_Spec( 'start_max', 'string' ),
			'query_timezone' => new Flexical_Admin_Object_Property_Spec( 'timezone', 'string' ),
			'query_start_min_relative' => new Flexical_Admin_Object_Property_Spec( 'start_min_relative', 'string' ),
			'query_start_min_offset' => new Flexical_Admin_Object_Property_Spec( 'start_min_offset', 'int' ),
			'query_start_max_relative' => new Flexical_Admin_Object_Property_Spec( 'start_max_relative', 'string' ),
			'query_start_max_offset' => new Flexical_Admin_Object_Property_Spec( 'start_max_offset', 'int' ),
		);

		$info = new MVC_Dynamo_Info( 'flexical/model/query.php', 'Flexical_Model_Query' );

		$this->query_builder = new Flexical_Admin_Object_Builder( $map, $info );
	}

	private function build_query( $request_data )
	{
		if ( $this->query_builder == null )
		{
			$this->initialize_query_builder();
		}

		return $this->query_builder->build( $request_data );
	}
}

?>
